#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
# 

"""
File: run12.py
Author: zhangyang(zhangyang40@baidu.com)
Date: 2018/1/5 0005 14:04
"""
"""
输入两个单调递增的链表，输出两个链表合成后的链表，
当然我们需要合成后的链表满足单调不减规则。
"""


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        if pHead1 is None:
            return pHead2
        elif pHead2 is None:
            return pHead1
        pMergeHead = None
        current = None

        while pHead1 is not None and pHead2 is not None:
            if pHead1.val <= pHead2.val:
                if pMergeHead is None:
                    pMergeHead = current = pHead1
                else:
                    current.next = pHead1
                    current = current.next
            else:
                if pMergeHead is None:
                    pMergeHead = current = pHead2
                else:
                    current.next = pHead2
                    current = current.next

        if pHead1 is None:
            current.next = pHead2
        else:
            current.next = pHead1

        return pMergeHead
